<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.mapper.${className}Mapper" >

    <resultMap id="BaseResultMap" type="${packageName}.model.${className}" >
        <#list columns as column>
        <#if column.name=='id'>
        <id column="id" property="id" jdbcType="BIGINT"/>
        <#else>
        <result column="${column.name}" property="${column.javaField}" jdbcType="${column.newJdbcType}"/>
        </#if>
        </#list>
    </resultMap>

    <sql id="Base_Column_List" >
        <#list columns as column>
        ${column.name}<#if column_has_next>,</#if>
        </#list>
    </sql>

    <select id="selectSortPage" resultMap="BaseResultMap" parameterType="${packageName}.model.${className}">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableName}
        <where>
            del_flag = '0'
            <if test="id != null">AND id = ${"#"}{id}</if>
            <#list columns as column>
            <#-- 如果不是基类属性 -->
            <#if column.isNotBaseField && column.name!="id">
            <if test="${column.javaField} != null">AND ${column.name} = ${"#"}{${column.javaField}}</if>
            </#if>
            </#list>
        </where>
        ORDER BY
        <if test="sortName != null and sortOrder != null">
            ${"$"}{sortName} ${"$"}{sortOrder},
        </if>
        id DESC
    </select>

    <select id="select" resultMap="BaseResultMap" parameterType="${packageName}.model.${className}">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableName}
        <where>
            del_flag = '0'
            <if test="id != null">AND id = ${"#"}{id}</if>
            <#list columns as column>
            <#-- 如果不是基类属性 -->
            <#if column.isNotBaseField && column.name!="id">
            <if test="${column.javaField} != null">AND ${column.name} = ${"#"}{${column.javaField}}</if>
            </#if>
            </#list>
        </where>
    </select>

    <insert id="insert" parameterType="${packageName}.model.${className}">
        INSERT INTO ${tableName}
        <trim prefix="(" suffixOverrides="," suffix=")">
            <#list columns as column>
            <if test="${column.javaField} != null">${column.name},</if>
            </#list>
        </trim>
        <trim prefix="VALUES (" suffixOverrides="," suffix=")">
            <#list columns as column>
            <if test="${column.javaField} != null">${"#"}{${column.javaField}},</if>
            </#list>
        </trim>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ${tableName}
        (
            <#list columns as column>
            ${column.name}<#if column_has_next>, </#if>
            </#list>
        )
        VALUES
        <foreach item="item" index="index" collection="list" separator=",">
            (
                <#list columns as column>
                ${"#"}{item.${column.javaField}}<#if column_has_next>, </#if>
                </#list>
            )
        </foreach>
    </insert>

    <update id="delete" parameterType="java.lang.Long">
        UPDATE ${tableName}
        SET del_flag = '1'
        WHERE id = ${"#"}{id}
    </update>

    <update id="deleteBatchByIDs" parameterType="java.util.List">
        UPDATE ${tableName}
        SET del_flag = '1'
        WHERE id
        <foreach item="id" index="index" collection="list" open="IN (" separator="," close=")">
            ${"#"}{id}
        </foreach>
    </update>

    <update id="update" parameterType="${packageName}.model.${className}">
        UPDATE ${tableName}
        <set>
            <#list columns as column>
            <#if (column.name != 'id')>
            <if test="${column.javaField} != null">${column.name} = ${"#"}{${column.javaField}},</if>
            </#if>
            </#list>
        </set>
        WHERE id = ${"#"}{id}
    </update>

    <select id="getOne" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableName}
        WHERE id = ${"#"}{id}
    </select>

    <select id="get" resultMap="BaseResultMap" parameterType="${packageName}.model.${className}">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${tableName}
        WHERE id = ${"#"}{id}
    </select>

</mapper>